package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@SuppressWarnings("all")
/**
 *@ClassName SetMealServiceImpl
 *@Description TODO
 *@Author @O_o  GW__
 *@Date 2025/5/26 11:30
 *@Version 1.0
 */
@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private SetMealDishMapper  setMealDishMapper;
//
    @Override
    @Transactional
    public void addMeal(SetmealDTO setmealDTO) {
//        新增数据到setmeal表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setMealMapper.insertSetmeal(setmeal);
        Long setmealId = setmeal.getId();
        log.info("id"+setmealId);
//        新增数据到setmeal_dish表
        for (SetmealDish setmealDish : setmealDTO.getSetmealDishes()) {
            setmealDish.setSetmealId(setmealId);
            setMealDishMapper.insertSetmealDish(setmealDish);
        }
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
//        开始分页查询
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
//        执行查询
        Page<Setmeal> page = setMealMapper.pageQuery(setmealPageQueryDTO);

        return new PageResult<>(page.getTotal(), page.getResult());
    }

    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        //        判断当前套餐是否能够删除--是否有在售菜品？
        for (Long id : ids) {
            Setmeal setmeal = setMealMapper.getById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                /**
                 // * 抛出不允许删除操作的异常
                 // * 当尝试删除一个正在销售的菜套餐时，抛出此异常
                 // * 这是为了防止正在销售的套餐被误删除，确保数据的一致性和完整性
                 // */
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
//        删除setMeal表中的数据
        setMealMapper.deleteSetMeal(ids);
//        删除setMeal_dish表数据
        setMealDishMapper.deleteSetMealDishs(ids);
    }

    @Override
    @Transactional
    public void updateSetMeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setMealMapper.update(setmeal);
        setMealDishMapper.deleteSetMealDish(setmeal.getId());
        for (SetmealDish setmealDish : setmealDTO.getSetmealDishes()) {
            setmealDish.setSetmealId(setmealDTO.getId());
            setMealDishMapper.insertSetmealDish(setmealDish);
        }

    }

    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = setMealMapper.getSetMealById(id);

        return setmealVO;
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        setMealMapper.startOrStop(status,id);
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setMealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setMealMapper.getDishItemBySetmealId(id);
    }
}
